package dataStructure.sort.selectSort;

import java.util.Arrays;

/**
 * @author lhn
 * @date 2024-08-29 15:42
 */

public class SelectSort {
    public static void main(String[] args) {
//        int[] arr = {1, 3, 2, 5, 4, 7, 6};
//        int[] arr = {3, 2, 5, 4, 7, 6};
        int[] arr = {101, 34, 119, 1, -1, 90, 223};
        System.out.println("排序前：");
        System.out.println(Arrays.toString(arr));
        sort(arr);
        System.out.println("排序后：");
        System.out.println(
                Arrays.toString(arr)
        );
    }

    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int index = i; // 第几趟排序就是找arr[i]位置上的值
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[index] > arr[j]) {
                    index = j;
                }
            }
            if (index != i) {
                int temp = arr[i];
                arr[i] = arr[index];
                arr[index] = temp;
            }
            System.out.println("第" + (i + 1) + "次排序：");
            System.out.println(Arrays.toString(arr));
        }
    }
}
